package io.bluerain.core;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

/**
 * Created by wscl on 2015/6/14.
 */
public class Http {

    public static String buildGetParamsStr(Map<String, Object> params) {
        StringBuilder paramsStr = null;
        try {

            paramsStr = new StringBuilder();
            boolean firstFlag = true;
            String con = "?";
            for (Map.Entry<String, Object> ps : params.entrySet()) {
                String key = URLEncoder.encode(ps.getKey(), "UTF-8");
                Object value = URLEncoder.encode(ps.getValue().toString(),
                        "UTF-8");
                paramsStr.append(con + key + "=" + value);
                if (firstFlag) {
                    con = "&";
                    firstFlag = false;
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return paramsStr.toString();
    }
}
